﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using Iprojects.Data;
using System.Linq;
using System.Data.Entity;
namespace Iprojects.Forms
{
    public partial class FamilyPersonAdd : Form
    {

        bool edit = false;
        ZakahEntities db = new ZakahEntities();
        Person _husband = new Person();
        Person _wife = new Person();
        FamilyPerson newFamilyRelation = new FamilyPerson();
        public FamilyPersonAdd()
        {
            InitializeComponent();
          
            IQueryable<City> query = db.Cities.Where(x => x.ID != 0);
            drpCity.DataSource = query;
            drpCity.DisplayMember = "City1";
            drpCity.ValueMember = "ID";
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // validate inputs  
            // add inputs to database 
           
           // FamilyPerson _family = new FamilyPerson();
            if (!edit)
            {
                newFamilyRelation = new FamilyPerson();
                _husband = new Person();
                _wife = new Person();

            }

            newFamilyRelation.Husbandid = txtHusbandid.Text;
            newFamilyRelation.wifeid = txtwifeid.Text;
            newFamilyRelation.street = txtStreet.Text;
            newFamilyRelation.CityID = Convert.ToInt32(drpCity.SelectedValue);
            newFamilyRelation.HomeNumber = txtHomeNum.Text;
            newFamilyRelation.POBOX = txtPOBOX.Text;
            newFamilyRelation.mikod = Convert.ToInt32(txtMikod.Text);
            newFamilyRelation.Telephone = txtTelephone.Text;
            newFamilyRelation.cellnumber = txtCell.Text;
            newFamilyRelation.childnumunder18 = Convert.ToInt32(txtChildesUnder18.Text);
            newFamilyRelation.childNumabove18 = Convert.ToInt32(txtChiledabove18.Text);
            newFamilyRelation.workType = txtWork.Text;
            newFamilyRelation.Income = Convert.ToInt32(txtIncome.Text);
            if (string.Empty != txtOjra.Text)
            {
                newFamilyRelation.Mashkanta_Ojra = Convert.ToInt32(txtOjra.Text);
            }
            newFamilyRelation.ownHouse = chkOwnHouse.Checked;
            if (edit)
            {

               // db.ObjectStateManager.GetObjectStateEntry(newFamilyRelation).SetModified();
                try
                {
                    db.Attach(newFamilyRelation);
                }
                catch { }
                 
                 

            }
            else
            {
                db.AddToFamilyPersons(newFamilyRelation);
            }
            db.SaveChanges();
            
            /*db.SaveChanges();*/
            _husband.FamilyID = newFamilyRelation.id;
            _husband.IDnum = txtHusbandid.Text;
            _husband.FirstName = txtHusFirstname.Text;
            _husband.FatherName = txtHusFatherName.Text;
            _husband.LastName = txtHusFamily.Text;
            if (!edit)
            {
                db.AddToPersons(_husband);
            }
            else
            {
            //    db.ObjectStateManager.GetObjectStateEntry(_currentPerson).SetModified();
                
                try
                {
                    db.Attach(_husband);
                }
                catch { }
            }

            _wife.FamilyID = newFamilyRelation.id;
            _wife.IDnum = txtwifeid.Text;
            _wife.FirstName = txtWifeFirstname.Text;
            _wife.FatherName = txtWifeFatherName.Text;
            _wife.LastName = txtWifeFamily.Text;
            if (!edit)
            {
                db.AddToPersons(_wife);
             //   db.AddToPersons(_currentPerson);
            }
            else
            {
                try
                {
                    db.Attach(_wife);
                }
                catch { }
                
               
            }
           
            db.SaveChanges();       
                
            btnAddFamily.Enabled =false;
            MessageBox.Show("تم اضافة العائلة بنجاح");
            // we should add id number for husband and wife 
            //  this.FillGridMySites(this.oMySites.GetDataList(tblMySitesDB.lists.All, tblMySitesDB.Where.None, tblMySitesDB.Order.ByID));
          
        }

       

        void txtWifeId_Leave(object sender, System.EventArgs e)
        {
            // check if there is id like that at table FamilyPerson
            //if have a relation in family person then
            bool isthereahusbandinsystem = false;
            if (txtwifeid.Text.Length > 8)
            {
              
                ZakahEntities db = new ZakahEntities();
                var familyrelation = db.FamilyPersons.FirstOrDefault(x => (x.Husbandid == txtwifeid.Text) || (x.wifeid == txtHusbandid.Text));
                if (null != familyrelation)
                {
                    isthereahusbandinsystem = true;
                }
                else
                {
                    isthereahusbandinsystem = false;
                
                }
            }

            if (isthereahusbandinsystem)
            {
                //  isthereahusbandinsystem
                MessageBox.Show(" الزوج موجود وتابع لعاءلة اخرى");
                // clear id  
                txtwifeid.Text = string.Empty;
                txtwifeid.Focus();

            }
            
         
            
        }
        void txtHusbandid_Leave(object sender, System.EventArgs e)
        {
             // check if there is id like that at table FamilyPerson
            //if have a relation in family person then
            bool isthereahusbandinsystem = false;
            if (txtHusbandid.Text.Length > 8)
            {
               //isthereahusbandinsystem= this.oPerson.ExisPersonByIDnumber(txtid.Text);

             
               var familyrelation = db.FamilyPersons.FirstOrDefault(x => (x.Husbandid == txtHusbandid.Text) || (x.wifeid == txtHusbandid.Text));




               if (null!= familyrelation)
               {
                   isthereahusbandinsystem = true;
                   
               }
               else
               {
                   isthereahusbandinsystem = false;
                   edit = false;
                   btnAddFamily.Enabled = true;
                  
               }
            }

                if (isthereahusbandinsystem)
            {
                //  isthereahusbandinsystem
           //     MessageBox.Show("تعديل");
                edit = true;
                btnAddFamily.Enabled = true;

              
                _husband = db.Persons.FirstOrDefault(x => x.IDnum == txtHusbandid.Text);
                newFamilyRelation = db.FamilyPersons.FirstOrDefault(x => x.id == _husband.FamilyID);
                _wife = db.Persons.FirstOrDefault(x => x.IDnum == newFamilyRelation.wifeid);

               txtHusbandid.Text = newFamilyRelation.Husbandid ;
                 txtwifeid.Text =newFamilyRelation.wifeid ;
                 txtStreet.Text = newFamilyRelation.street;
                drpCity.SelectedValue = newFamilyRelation.CityID ;
                txtHomeNum.Text = newFamilyRelation.HomeNumber;
                 txtPOBOX.Text=newFamilyRelation.POBOX ;
                 txtMikod.Text = newFamilyRelation.mikod.ToString();
               txtTelephone.Text= newFamilyRelation.Telephone;
                txtCell.Text=newFamilyRelation.cellnumber ;

                if (null != newFamilyRelation.childnumunder18)
                {
                    txtChildesUnder18.Text = newFamilyRelation.childnumunder18.ToString();
                }
                if (null != newFamilyRelation.childNumabove18)
                {
                    txtChiledabove18.Text = newFamilyRelation.childNumabove18.ToString();
                }
                txtWork.Text = newFamilyRelation.workType;

                if (null != newFamilyRelation.Income)
                {
                    txtIncome.Text = newFamilyRelation.Income.ToString();
                }
                if (null != newFamilyRelation.Mashkanta_Ojra)
                {
                    txtOjra.Text = newFamilyRelation.Mashkanta_Ojra.ToString();
                }

                chkOwnHouse.Checked = (bool)newFamilyRelation.ownHouse;
                

                /*db.SaveChanges();*/
                //newFamilyRelation.id=_husband.FamilyID;
               txtHusbandid.Text= _husband.IDnum ;
               txtHusFirstname.Text =_husband.FirstName;
               txtHusFatherName.Text = _husband.FatherName;
               txtHusFamily.Text =_husband.LastName;

               
            //    newFamilyRelation.id= _wife.FamilyID ;
                 txtwifeid.Text =_wife.IDnum ;
                txtWifeFirstname.Text = _wife.FirstName  ;
               txtWifeFatherName.Text = _wife.FatherName  ;
               txtWifeFamily.Text=  _wife.LastName  ;
                
                // clear id  
               // txtHusbandid.Text = string.Empty;
               // txtwifeid.Focus();

            }
           
         
           
        }
        }

   
    }



